package com.yakuj3.android.comptadroid.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yakuj3.android.comptadroid.Model;
import com.yakuj3.android.comptadroid.R;

public class MyDrawerAdapter extends ArrayAdapter<Model> {

	Context context;
	private ArrayList<Model> TextValue = new ArrayList<Model>();

	public MyDrawerAdapter(Context context, ArrayList<Model> TextValue) {
	    super(context, R.layout.drawer_listview_item, TextValue);
	    this.context = context;
	    this.TextValue= TextValue;

	}

	@Override
	public View getView(int position, View coverView, ViewGroup parent) {
	    // TODO Auto-generated method stub

	    LayoutInflater inflater = (LayoutInflater) context
	            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    
	    View rowView = null;
	    if (!TextValue.get(position).isGroupHeader())
	    {
	    	rowView = inflater.inflate(R.layout.drawer_listview_item,
		            parent, false);

		    ImageView imvw = (ImageView) rowView.findViewById(R.id.drawer_item_icon);
		    imvw.setImageResource(TextValue.get(position).getIcon());
		    
		    TextView text1 = (TextView)rowView.findViewById(R.id.text1);
		    text1.setText(TextValue.get(position).getTitle());
		    
		    TextView counter = (TextView)rowView.findViewById(R.id.drawer_item_counter);
		    if (TextValue.get(position).hasCounter())
		    {
		    	counter.setText(TextValue.get(position).getCounter());
		    	counter.setBackground(getContext().getResources().getDrawable(R.drawable.rectangle));
		    }
	    }
	    else
	    {
	    	rowView = inflater.inflate(R.layout.drawer_header_item, parent, false);
	    	TextView header = (TextView) rowView.findViewById(R.id.drawer_header);
	    	header.setText(TextValue.get(position).getTitle());
	    }
	    
	    return rowView;

	}
	
	public boolean setCounterForMonth(String month, String counter)
	{
		for (int i = 0; i < TextValue.size(); i++)
		{
			if (TextValue.get(i).getTitle().equalsIgnoreCase(month))
			{
				TextValue.get(i).setCounter(counter);
				TextValue.get(i).setHasCounter(true);
				return true;
			}
		}
		return false;
	}
	
	@Override
	public boolean isEnabled(int position) {
		// TODO Auto-generated method stub
		if (TextValue.get(position).isGroupHeader())
			return false;
		return true;
	}
}
